/*
 * Copyright (C) 2010-2014 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package trs.wmata.api;

import app.transittool.model.Agency;

import trs.wmata.Train;
import static trs.wmata.api.ApiXml.AIM_PREDICTION_TRAIN_INFO;
import static trs.wmata.api.ApiXml.DESTINATION;
import static trs.wmata.api.ApiXml.DESTINATION_CODE;
import static trs.wmata.api.ApiXml.DESTINATION_NAME;
import static trs.wmata.api.ApiXml.GROUP;
import static trs.wmata.api.ApiXml.LINE;
import static trs.wmata.api.ApiXml.MIN;
import static trs.wmata.api.Util.toInt;

import java.util.ArrayList;

import org.xml.sax.Attributes;

public final class NextTrain extends ApiResponse {
    private Train train = null;
    private final ArrayList<Train> trains = new ArrayList<Train>();
    private final Agency agency;

    public NextTrain(final Agency agency) {
        this.agency = agency;
    }

    @Override
    public void startElement(final String lName, final Attributes atts) {
        if (AIM_PREDICTION_TRAIN_INFO.equals(lName)) {
            train = new Train(agency);

            // } else if (Car.equals(lName)) {
            // } else if (Destination.equals(lName)) {
            // } else if (DestinationCode.equals(lName)) {
            // } else if (DestinationName.equals(lName)) {
            // } else if (Group.equals(lName)) {
            // } else if (Line.equals(lName)) {
            // } else if (LocationCode.equals(lName)) {
            // } else if (LocationName.equals(lName)) {
            // } else if (Min.equals(lName)) {
        }
    }

    @Override
    public void endElement(final String lName, final StringBuilder buff) {
        if (AIM_PREDICTION_TRAIN_INFO.equals(lName)) {
            if (train != null) {
                trains.add(train);
                train = null;
            }
        } else if (train != null) {
            final String str = buff.toString().trim();

            /*
             * if (Car.equals(lName)) { // train.setCars(toInt(str)); } else
             */
            if (DESTINATION.equals(lName)) {
                train.setDestination(str);
            } else if (DESTINATION_CODE.equals(lName)) {
                train.setDestinationCode(str);
            } else if (DESTINATION_NAME.equals(lName)) {
                train.setDestinationName(str);
            } else if (GROUP.equals(lName)) {
                train.setGroup(toInt(str));
            } else if (LINE.equals(lName)) {
                train.setLine(str);

                // } else if (LocationCode.equals(lName)) {
                // } else if (LocationName.equals(lName)) {
            } else if (MIN.equals(lName)) {
                try {
                    train.setMinutes(Integer.valueOf(str));
                } catch (final NumberFormatException e) {
                    train.setMinutes(0);
                    train.setStatus(str);
                }
            }
        }
    }

    public ArrayList<Train> getTrains() {
        return trains;
    }

    @Override
    public boolean doCaptureChars() {
        return train != null;
    }
}
